import router from "./index";
import useMain  from "../stores/use-main";
import {TokenUtil} from "@lowcode/jpaas-base-lib";
//路由守卫
router.beforeEach((to, from, next) => {
    //应用ID
    let appId="";
    if(to.query && to.query.appId){
        appId=to.query.appId;
    }
    //token
    const  token = TokenUtil.getToken();
    const store = useMain();
    store.setAppId(appId);
    if(token){
        store.setToken(token);
        if (from.path != '/') {
            next();
            return;
        }
        if(to.path=="/"){
            let menuName="screenViewManage";
            store.setSelectedKey(menuName);
            next({name:menuName,query:to.query});
            return;
        }
        store.setSelectedKey(to.name);
        next();
    }else if(to.query && to.query.token) {
        //iframe嵌套传入token过来时候
        let toke = to.query.token;
        TokenUtil.setToken(toke);
        store.setToken(toke);
        next();
    }
    else {
        let href = window.location.href ;
        let mainUrl= import.meta.env.JPAAS_MAIN_URL;
        if(!href.includes(mainUrl)){
            window.location.href =  `${mainUrl}?redirect=${href}`
        }
    }
})
